﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class AuditBillingMaster : System.Web.UI.MasterPage
{
    protected void Page_Init(object sender, EventArgs e)
    {
        if (SessionInfo.prop_getObj.EmployeeID == null || SessionInfo.prop_getObj.EmployeeID == 0)
        {
            FormsAuthentication.SignOut();
            Session.Clear();
            Session.Abandon();
            FormsAuthentication.RedirectToLoginPage();
            Response.Redirect("login.aspx");
        }

        GetMenuData();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    private void GetMenuData()
    {
        PLMstUser objEmp = new PLMstUser();
        objEmp.MstUserID = SessionInfo.prop_getObj.EmployeeID;
        objEmp.IsAdmin = SessionInfo.prop_getObj.IsEmployeeAdmin;

        List<PLMstFormMasters> objList = new BL_User().BL_fnGetEmployeeMenu(objEmp);
        List<PLMstFormMasters> objSubList = objList.Where(s => s.ParentForm.GetValueOrDefault() == 0).ToList();
        foreach (PLMstFormMasters objSubListDet in objSubList)
        {
            HtmlGenericControl htmlGenLI = fnGetHTMLCntrl(menu, "li", "", "<a href='#'>" + objSubListDet.FormName + "</a>", "");

            AddChildItems(objList, htmlGenLI, objSubListDet.FormID, 0);
        }
        //}
    }

    /// <summary>
    /// Populate the childmenucontrols as per given datavalues
    /// </summary>
    /// <param name="pObjFormList"></param>
    /// <param name="menuItem"></param>
    private void AddChildItems(List<PLMstFormMasters> pObjFormList, HtmlGenericControl menuItem, int pIntRowId, int pHeaderCount)
    {
        List<PLMstFormMasters> objChildList = pObjFormList.Where(s => s.ParentForm == pIntRowId).ToList();
        HtmlGenericControl htmlGenUL = fnGetHTMLCntrl(menuItem, "ul", "", "", "");
        foreach (PLMstFormMasters objChild in objChildList)
        {
            HtmlGenericControl htmlGenLI = fnGetHTMLCntrl(htmlGenUL, "li", "", "<a href='" + (string.IsNullOrEmpty(objChild.FormURL) ? "javascript:void(0);" : objChild.FormURL) + "'>" + objChild.FormName + "</a>", "");
            List<PLMstFormMasters> objSubChildList = pObjFormList.Where(s => s.ParentForm == objChild.FormID).ToList();
            if (objSubChildList.Count > 0)
            {
                AddChildItems(pObjFormList, htmlGenLI, objChild.FormID, 0);
            }
        }

    }

    private HtmlGenericControl fnGetHTMLCntrl(HtmlGenericControl pHTMLParent, string pStrTag, string pStrClass, string pStrInnerHTML, string pStrhref)
    {
        HtmlGenericControl temp1 = new HtmlGenericControl(pStrTag);

        if (pHTMLParent == null)
            return temp1;
        if (pStrClass != "")
            temp1.Attributes.Add("class", pStrClass);
        if (pStrInnerHTML != "")
            temp1.InnerHtml = pStrInnerHTML;
        if (pStrhref != "")
            temp1.Attributes.Add("href", pStrhref);
        pHTMLParent.Controls.Add(temp1);
        return temp1;
    }


}
